MCU = -mmcu=atmega128
CC = avr-gcc
CFLAGS = $(MCU) -Wall -W -Wstrict-prototypes
#CFLAGS += -O3
#CFLAGS += -O2
CFLAGS += -Os
#CFLAGS += -mrtl
CFLAGS += -Wa,-ahlms=$(<:.c=.lst)
CFLAGS += -g
LD = avr-gcc

#Comment out all but the applicable target micro, NB largest possible boot block sizes are used.
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x1800# mega8 2kB Boot Block
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x3800# mega16 2kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x7000# mega32 4kB BB (DES code will fit)
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0xE000# mega64 8kB BB (DES code will fit)
LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x1F000# meag128 8kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x3800# mega162 2kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x3800# mega163 2kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x3800# mega169 2kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x1800# mega8515 2kB BB
#LDFLAGS = $(MCU) -Wl,-M,--section-start=.text=0x1800# mega8535 2kB BB

PRG = uisp -dprog=stk200 -dlpt=0x378 -dpart=ATmega128 -v=3

OBJS = bootloader.o

LST = bootloader.lst
OUT = bootloader.elf
SREC = bootloader.src
IHEX = bootloader.hex
MAP = bootloader.map
FUSES = fuses.mot

all: $(SREC) $(IHEX)

$(SREC): $(OUT)
	avr-objcopy --output-target=srec $(OUT) $(SREC)

$(IHEX): $(OUT)
	avr-objcopy --output-target=ihex $(OUT) $(IHEX)

flash-erase:
	$(PRG) --erase

flash-write: $(SREC)
	$(PRG) --upload --verify if=$(SREC)

fuse: $(FUSES)
	$(PRG) --upload --verify if=$(FUSES)

flash: $(SREC)
	$(PRG) --erase --upload --verify if=$(SREC)

$(OUT): $(OBJS)
	$(LD) $(OBJS) -o $(OUT) $(LDFLAGS) > $(MAP)

clean:
	rm -f $(OBJS) $(OUT) $(SREC) $(IHEX) $(MAP) $(LST)

